package com.fbli.test.controller;

import com.fbli.test.common.entity.Authority.AuthorityResMsg;
import com.fbli.test.common.result.ResultParam;
import com.fbli.test.common.result.ResultSend;
import com.fbli.test.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"权限管理"})
@RestController
public class AuthorityController {
    @Autowired
    AuthorityService authorityService;
    @ApiOperation("为用户添加权限")
    @PostMapping("/addAuthority")
    @RequiresPermissions("admin")
    public ResultParam addAuthority(@RequestBody AuthorityResMsg authorityResMsg){
        String[] prems=authorityResMsg.getPrems();
        long userId=authorityResMsg.getUserId();
        String role = authorityResMsg.getRole();
        if (prems==null || prems.length<=0 || userId<=0 || role==null || role.length()<=0){
            return ResultSend.fail("输入不能存在空值");
        }else {
            boolean b = authorityService.addAuthority(prems, userId,role);
            if (b){
                return ResultSend.success("权限添加成功");
            }else {
                return ResultSend.fail("用户不存在");
            }
        }
    }
}
